﻿// <copyright file="SettingsPage.xaml.cs" company="Mooville">
//   Copyright © 2010 Roger Deetz. All rights reserved.
// </copyright>

namespace Mooville.QUno.Phone
{
    using System;
    using System.ComponentModel;
    using System.Globalization;
    using System.IO.IsolatedStorage;
    using System.Reflection;
    using System.Windows;
    using Microsoft.Phone.Controls;
    using Mooville.QUno.Phone.ViewModel;
    using Mooville.QUno.ViewModel;

    public partial class SettingsPage : PhoneApplicationPage
    {
        private OptionsViewModel viewModel;

        public SettingsPage()
        {
            this.InitializeComponent();

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                this.viewModel = new OptionsViewModel(new PhoneSettingsProvider());
                this.Loaded += this.Page_Loaded;
                this.Unloaded += this.Page_Unloaded;
            }
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            this.viewModel.LoadSettings();

            string fullName = Assembly.GetExecutingAssembly().FullName;
            string version = fullName.Split(',')[1].Split('=')[1];
            this.textVersion.Text = String.Format(CultureInfo.CurrentCulture, this.textVersion.Text, version.ToString());

            return;
        }

        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            this.viewModel.SaveSettings();

            return;
        }

        private void Ok_Click(object sender, EventArgs e)
        {
            if (this.NavigationService.CanGoBack)
            {
                this.NavigationService.GoBack();
            }

            return;
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            if (this.NavigationService.CanGoBack)
            {
                this.NavigationService.GoBack();
            }

            return;
        }
    }
}
